package com.iter.devbox.json;

import java.io.IOException;
import java.util.Map;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.iter.devbox.json.Recharge.Entity;

/**
 * 目前主流的JSON解析主要使用4种类库
 * 1、sf-json 官网：http://json-lib.sourceforge.net/，很老，2011年之后没有更新，随机访问最方便
 * 2、Jackson 官网http://www.codehaus.org/已经转移至github：https://github.com/codehaus/jackson
 * 3、fastjson 阿里巴巴的
 * 4、Gson 谷歌的
 * 
 * @author Jimly
 *
 */
public class JSONClient {

	public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
		fastJson();
		jackson();
		gson();
	}
	
	/**
	 * fastjson解析json
	 * 
	 */
	public static void fastJson () {
		String json = "{\"data\":[{\"randomValidateId\":\"6d785708-bc66-4a69-83a5-18321f34642a-1\",\"requestId\":\"FF20160401173804917173569\",\"result\":\"00000\"},{\"randomValidateId\":\"6d785708-bc66-4a69-83a5-18321f34642a-2\",\"requestId\":\"FF20160401173804917173569\",\"result\":\"00000\"}]}";
		
		JSONObject jsonObject = JSONObject.parseObject(json);
		JSONArray jsonArray = jsonObject.getJSONArray("data");
		for (int i=0; i<jsonArray.size(); i++) {
			JSONObject jObj = jsonArray.getJSONObject(i);
			String randomValidateId = jObj.getString("randomValidateId");
			System.out.println(randomValidateId);
		}
	}
	
	/**
	 * jackson解析json
	 * 
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	public static void jackson () throws JsonParseException, JsonMappingException, IOException {
		String json = "{\"entity\":[{\"randomValidateId\":\"6d785708-bc66-4a69-83a5-18321f34642a-1\",\"requestId\":\"FF20160401173804917173569\",\"result\":\"00000\"},{\"randomValidateId\":\"6d785708-bc66-4a69-83a5-18321f34642a-2\",\"requestId\":\"FF20160401173804917173569\",\"result\":\"00000\"}]}";
		
		ObjectMapper objectMapper = new ObjectMapper();
		Recharge ret = objectMapper.readValue(json, Recharge.class);
		Entity[] entity = ret.getEntity();
		for (Entity e : entity) {
			System.out.println(e.getRandomValidateId());
		}
		
		String json_2 = "{\"randomValidateId\":\"6d785708-bc66-4a69-83a5-18321f34642a-1\",\"requestId\":\"FF20160401173804917173569\",\"result\":\"00000\"}";
		
		ObjectMapper objectMapper_2 = new ObjectMapper();
		Entity entity_2 = objectMapper_2.readValue(json_2, Entity.class);
		System.out.println(entity_2.getRandomValidateId());
		
		//新版Jackson解析json（从2.0开始改了包名）
		com.fasterxml.jackson.databind.ObjectMapper xin_objectMapper = new com.fasterxml.jackson.databind.ObjectMapper();
		Recharge xin_ret = xin_objectMapper.readValue(json, Recharge.class);
		Entity[] xin_entity = xin_ret.getEntity();
		for (Entity e : xin_entity) {
			System.out.println("新版Jackson解析：" + e.getRandomValidateId());
		}
	}
	
	/**
	 * gson解析json
	 * 
	 */
	public static void gson () {
		String json = "{\"randomValidateId\":\"6d785708-bc66-4a69-83a5-18321f34642a-1\",\"requestId\":\"FF20160401173804917173569\",\"result\":\"00000\"}";
		Gson gson = new Gson();
		Entity entity = gson.fromJson(json, Entity.class);
		System.out.println("gson:" + entity.getRandomValidateId());
		
		//方法二
		Map<String,String> map = gson.fromJson(json, new TypeToken<Map<String,String>>() {}.getType());
		System.out.println("gson:" + map.get("requestId"));
	}
}
